package org.nepu.su.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lboot.core.auth.anno.AuthLogin;
import org.lboot.core.domain.ResponseDTO;
import org.lboot.core.exception.BusinessException;
import org.lboot.jpa.utils.JpaDataUtil;
import org.nepu.sc.dict.SafeRateDict;
import org.nepu.sc.dict.SafeRateDictService;
import org.nepu.sc.dict.params.SafeRateDictQueryParams;
import org.nepu.sc.domain.DataVO;
import org.nepu.sc.domain.RiskResult;
import org.nepu.sc.ignore.SafeIgnoreUnit;
import org.nepu.sc.ignore.SafeIgnoreUnitService;
import org.nepu.sc.ignore.params.SafeIgnoreUnitParams;
import org.nepu.sc.ignore.params.SafeIgnoreUnitQueryParams;
import org.nepu.sc.math.MathUtil;
import org.nepu.sc.mx.SafeMx;
import org.nepu.sc.mx.SafeMxService;
import org.nepu.sc.mx.params.SafeMxParams;
import org.nepu.sc.mx.params.SafeMxQueryParams;
import org.nepu.sc.unit.SafeUnit;
import org.nepu.sc.unit.SafeUnitService;
import org.nepu.sc.unit.params.SafeUnitParams;
import org.nepu.sc.unit.params.SafeUnitQueryParams;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("safe")
@AllArgsConstructor
@Api(tags = "安全评价忽略参数管理")
public class SafeIgnoreUnitController {
    SafeUnitService unitService;

    SafeIgnoreUnitService ignoreUnitService;

    SafeRateDictService dictService;

    SafeMxService mxService;


    @AuthLogin
    @PostMapping("ignoreUnits")
    @ApiOperation(value = "参数新增/更新")
    public ResponseDTO<Object> unitSet(@Validated @RequestBody SafeIgnoreUnitParams params){
        String taskId = params.getTaskId();
        String rateId = params.getRateId();
        List<SafeIgnoreUnit> units = ignoreUnitService.getByTaskIdAndRateId(taskId, rateId);
        // 根据RateId 匹配名词
        List<SafeRateDict> dicts = dictService.getAll();
        Map<String, String> dictMap = new HashMap<>();
        for (SafeRateDict dict:dicts){
            dictMap.put(dict.getRateId(), dict.getRateName());
        }
        // 获取父级别
        String parent = StrUtil.sub(rateId, 0, rateId.length()-1);
        if (units.isEmpty()){
            // 新建
            SafeIgnoreUnit ignoreUnit = new SafeIgnoreUnit();
            BeanUtil.copyProperties(params, ignoreUnit, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
            // 获取名称
            ignoreUnit.setRateName(dictMap.get(rateId));
            ignoreUnit.setParentId(parent);
            return ResponseDTO.succData(
                    ignoreUnitService.saveOne(ignoreUnit)
            );
        }
        else {
            // 获取第一个
            SafeIgnoreUnit unit = units.get(0);
            // 获取名称
            unit.setRateName(dictMap.get(rateId));
            return ResponseDTO.succData(
                    ignoreUnitService.updateOne(unit)
            );
        }
    }


    @AuthLogin
    @PostMapping("ignoreUnits/batch")
    @ApiOperation(value = "参数新增/更新(批量)")
    public ResponseDTO<Object> batchSet(@Validated @RequestBody List<SafeIgnoreUnitParams> list){
        for (SafeIgnoreUnitParams params:list){
            unitSet(params);
        }
        return ResponseDTO.succMsg("操作成功");
    }


    @AuthLogin
    @DeleteMapping("ignoreUnits/{id}")
    @ApiOperation(value = "参数删除")
    public ResponseDTO<Object> unitDelete(@PathVariable("id") String id){
        ignoreUnitService.deleteOne(id);
        return ResponseDTO.succMsg("删除成功");
    }


    @AuthLogin
    @GetMapping("ignoreUnits")
    @ApiOperation(value = "指标列表")
    public ResponseDTO<Object> dictList(SafeIgnoreUnitQueryParams params){
        Pageable pageable = JpaDataUtil.buildPageable(params);
        // 组合查询
        Specification<SafeIgnoreUnit> spec = (root, query, cb) -> {
            return cb.like(root.get("rateId"),params.getSearchKey()+"%");
        };
        if (Validator.isEmpty(params.getSearchKey())){
            return ResponseDTO.succData(
                    ignoreUnitService.getAll(pageable)
            );
        }else {
            return ResponseDTO.succData(
                    ignoreUnitService.getAll(pageable,spec)
            );
        }
    }


}
